package com.zkt.subject.infra.basic.entity;

import com.baomidou.mybatisplus.annotation.*;

import java.io.Serial;
import java.time.LocalDateTime;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 用户做题记录表
 * </p>
 *
 * @author 赵开泰
 * @since 2025-03-27
 */

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("subject_user_record")
public class SubjectUserRecord implements Serializable {
	
	@Serial
	private static final long serialVersionUID = 1L;
	
	/**
	 * 主键
	 */
	@TableId(value = "id")
	private Long id;
	
	/**
	 * 用户id
	 */
	@TableField("user_id")
	private Long userId;
	
	/**
	 * 题目id
	 */
	@TableField("subject_id")
	private Long subjectId;
	
	/**
	 * 题目名称
	 */
	@TableField("subject_name")
	private String subjectName;
	
	/**
	 * 题目难度
	 */
	@TableField("subject_difficult")
	private Integer subjectDifficult;
	
	/**
	 * 题目分数
	 */
	@TableField("subject_score")
	private Integer subjectScore;
	
	/**
	 * 用户获得分数
	 */
	@TableField("user_score")
	private Integer userScore;
	
	/**
	 * 本题耗时(秒)
	 */
	@TableField("use_time")
	private Integer useTime;
	
	/**
	 * 用户答案
	 */
	@TableField("user_answer")
	private String userAnswer;
	
	/**
	 * 标签id
	 */
	@TableField("label_id")
	private Long labelId;
	
	/**
	 * 标签ids
	 */
	private List<Long> labelIds;
	
	/**
	 * 创建人
	 */
	@TableField(value = "created_by", fill = FieldFill.INSERT)
	private String createdBy;
	
	/**
	 * 创建时间
	 */
	@TableField(value = "created_time", fill = FieldFill.INSERT)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private LocalDateTime createdTime;
	
	/**
	 * 更新人
	 */
	@TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
	private String updateBy;
	
	/**
	 * 更新时间
	 */
	@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private LocalDateTime updateTime;
	
	/**
	 * 逻辑删除
	 */
	@TableField(value = "is_deleted", fill = FieldFill.INSERT)
	private Integer isDeleted;
	
	public void setLabelIdList(String labelIdList) {
		if (labelIds == null || labelIds.isEmpty()) {
			this.labelIds = List.of();
		}
		this.labelIds = Arrays.stream(labelIdList.split(","))
				.map(String::trim)
				.map(Long::valueOf)
				.toList();
	}
}
